import CreateDatabase from "../components/databaseInfo.vue"
import { metadataCovert, metaDefaultConfg } from "./constant"
import { SqlBuilder } from "./SqlBuilder"
import {ScriptApi} from "../api/scriptApi"
class ResourceConn {
  constructor(opt, params) {
    this.opt = opt;
    this.params = params
    this.node = params.node;
    this.sqlBuilder = new SqlBuilder()
    this.scriptApi = new ScriptApi(opt.request,opt.message)
  }

  getMetadata() {
    const {
      connId,
      catalog,
      tableName
    } = this.node.data.properties
    const rowCovert = (d, row) => {
      d.name = row.SCHEMA_NAME;
      d.id = `${connId}_${d.name}`;
      d.type = 'CATALOG';
      d.properties.catalog = d.name;

    }
    return new Promise((resolve, reject) => {
      const executeParams = {
        script: this.sqlBuilder.selectCatalogs(),
        noPage:true
      };
      const params = {
        connId: this.node.data.properties.connId,
        executeParams: executeParams
      };
      this.scriptApi.singleExecute(params).then(res => {
        resolve(metadataCovert(this.opt, rowCovert, res, connId,catalog,tableName))
      }).catch(e => {
        reject(e)
      })
    })
  }

  getContextMenus() {
    let menus = [];
    const disabled = this.node && this.node.childrenLoaded;
    menus.push({
      label: "新建数据库",
      dividedTop: true,
      disabled: !disabled,
      click: (event, item) => {
        this.opt.bus.$emit(this.opt.config.CONTEXT_MENU_EVENT_KEY, {
          type: this.opt.config.CONTEXT_MENU_TYPE_DIALOG,
          title: '新建数据库',
          width: '350px',
          height: '300px',
          component: CreateDatabase,
          data: {
            connId: this.node.data.properties.connId,
            //charsets: characterSets

          }
        })
        return true;
      },
    })
    menus.push({
      label: "新建查询",
      click: () => {
        this.opt.bus.$emit(this.opt.config.CONTEXT_MENU_EVENT_KEY, {
          type: this.opt.config.CONTEXT_MENU_TYPE_TAGS_COM,
          title: '新建查询',
          id: this.node.data.id + "_sql_query",
          component: 'WorkbeanchSqlEditor',
          data: {
            connId: this.node.data.properties.connId,
            catalog: this.node.data.properties.catalog
          }
        })
        return true;
      },
    })
    return menus
  }
}

export {
  ResourceConn
}